<?php

/**
 * This is the model class for table "backup_schedule".
 *
 * The followings are the available columns in table 'backup_schedule':
 * @property integer $id_backup_schedule
 * @property string $name
 * @property string $id_group
 * @property string $cpanel_accounts
 * @property string $id_backup_locations
 * @property string $email
 * @property integer $delay
 * @property integer $status
 * @property integer $sun
 * @property integer $mon
 * @property integer $tue
 * @property integer $wed
 * @property integer $thur
 * @property integer $fri
 * @property integer $sat
 * @property integer $hour
 */
class BackupSchedule extends CActiveRecord
{
	const STATUS_ACTIVE = 1;
	const STATUS_SUSPENDED = 0;
	
	const Sun = 'Sun';
	const Mon = 'Mon';
	const Tue = 'Tue';
	const Wed = 'Wed';
	const Thur = 'Thu';
	const Fri = 'Fri';
	const Sat = 'Sat';
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BackupSchedule the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'backup_schedule';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('', 'required'),
			array('id_backup_schedule, delay, status, sun, mon, tue, wed, thur, fri, sat, hour', 'numerical', 'integerOnly'=>true),
			array('name, id_group, cpanel_accounts, id_backup_locations, email', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_backup_schedule, name, id_group, cpanel_accounts, id_backup_locations, email, delay, status, sun, mon, tue, wed, thur, fri, sat, hour', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'BackupLocation' => array(self::HAS_ONE, 'BackupLocations', array('id_backup_locations' => 'id_backup_locations')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_backup_schedule' => 'Id Backup Schedule',
			'name' => 'Name',
			'id_group' => 'Id Group',
			'cpanel_accounts' => 'Cpanel Accounts',
			'id_backup_locations' => 'Id Backup Locations',
			'email' => 'Email',
			'delay' => 'Delay',
			'status' => 'Status',
			'sun' => 'Sun',
			'mon' => 'Mon',
			'tue' => 'Tue',
			'wed' => 'Wed',
			'thur' => 'Thur',
			'fri' => 'Fri',
			'sat' => 'Sat',
			'hour' => 'Hour',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_backup_schedule',$this->id_backup_schedule);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('id_group',$this->id_group,true);
		$criteria->compare('cpanel_accounts',$this->cpanel_accounts,true);
		$criteria->compare('id_backup_locations',$this->id_backup_locations,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('delay',$this->delay);
		$criteria->compare('status',$this->status);
		$criteria->compare('sun',$this->sun);
		$criteria->compare('mon',$this->mon);
		$criteria->compare('tue',$this->tue);
		$criteria->compare('wed',$this->wed);
		$criteria->compare('thur',$this->thur);
		$criteria->compare('fri',$this->fri);
		$criteria->compare('sat',$this->sat);
		$criteria->compare('hour',$this->hour);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getScheduleDays()
	{
		$daysArr = array();
		if($this->sun == 1){
			$daysArr[] = self::Sun;
		}
		if($this->mon == 1){
			$daysArr[] = self::Mon;
		}
		if($this->tue == 1){
			$daysArr[] = self::Tue;
		}
		if($this->wed == 1){
			$daysArr[] = self::Wed;
		}
		if($this->thur == 1){
			$daysArr[] = self::Thur;
		}
		if($this->fri == 1){
			$daysArr[] = self::Fri;
		}
		if($this->sat == 1){
			$daysArr[] = self::Sat;
		}
		return $daysArr;
	}

	public function renderListActionButtons()
	{
		return '
			<div class="btn-group">
				<button class="btn btn-info dropdown-toggle actions" data-toggle="dropdown">
					'. t('buttons', 'Actions') .'
					<span class="caret"></span>
				</button>
				<ul class="dropdown-menu">
					<li class="backupschedule_edit"><a href="javascript:void(0);"><i class="radmin-icon radmin-pencil"></i> '. t('buttons', 'Edit') .'</a></li>
					<li class="backupschedule_delete"><a href="javascript:void(0);"><i class="radmin-icon radmin-remove"></i> '. t('buttons', 'Delete') .'</a></li>
				</ul>
				
				<div class="delete_confirm_btn hide">
					<a class="btn btn-small delete_confirm"><i class="icon-ok"></i>'. t('buttons', 'Confirm Delete') .'</a>
					<a class="btn btn-small delete_cancel"><i class="icon-remove"></i>'. t('buttons', 'Cancel') .'</a>
				</div>
			</div>
		';
	}
	
	public function __get($key)
	{
		$action = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
		if(method_exists($this, $action)){
			return $this->$action();
		}
		
		return parent::__get($key);
	}
	public function getStatusTxt()
	{
		return ($this->status == self::STATUS_ACTIVE)? 'Active' : 'Suspended';
	}
	public function getDaysTxt()
	{
		return implode(',', $this->getScheduleDays());
	}
	
	public function initDays($days)
	{
		$this->sun = 0;
		$this->mon = 0;
		$this->tue = 0;
		$this->wed = 0;
		$this->thur = 0;
		$this->fri = 0;
		$this->sat = 0;
		foreach ($days as $day){
			switch ($day){
				case self::Sun:
					$this->sun = 1;
					break;
				case self::Mon:
					$this->mon = 1;
					break;
				case self::Tue:
					$this->tue = 1;
					break;
				case self::Wed:
					$this->wed = 1;
					break;
				case self::Thur:
					$this->thur = 1;
					break;
				case self::Fri:
					$this->fri = 1;
					break;
				case self::Sat:
					$this->sat = 1;
					break;
			}
		}
	}
}